package me.dr0n3.skyAdopt.Listeners;

import java.util.LinkedList;
import java.util.Random;
import java.util.logging.Level;
import me.dr0n3.skyAdopt.Utils.AnimalHelper;
import me.dr0n3.skyAdopt.Utils.ConfigurationHelper;
import me.dr0n3.skyAdopt.Utils.MessageHelper;
import me.dr0n3.skyAdopt.Utils.SignHelper;
import me.dr0n3.skyAdopt.skyAdopt;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

/**
 * @author dr0n3
 * @date 16.04.2012
 */
public class skyPlayerListener implements Listener
{
    // <editor-fold defaultstate="collapsed" desc="Variablen">
    skyAdopt plugin;
    // </editor-fold>

    public skyPlayerListener(skyAdopt instance) 
    {
        plugin = instance;
    }
    
    @EventHandler
    public void onPlayerChangedWorld(PlayerChangedWorldEvent e)
    {
        Player player = e.getPlayer();
        
        LinkedList<Wolf> respawnWolfes = new LinkedList<>();
        LinkedList<Ocelot> respawnCats = new LinkedList<>();
        
        for(Wolf w : AnimalHelper.hmWolfes.get(player))
        {
            if(!w.isSitting())
            {
                respawnWolfes.add(w);
                AnimalHelper.hmWolfes.get(player).remove(w);
            }
        }
        
        for(Ocelot o : AnimalHelper.hmCats.get(player))
        {
            if(!o.isSitting())
            {
                respawnCats.add(o);
                
                AnimalHelper.hmCats.get(player).remove(o);
            }
        }
        
        for(Wolf w : respawnWolfes)
        {
            Wolf wo = (Wolf) player.getWorld().spawnCreature(player.getLocation(), EntityType.WOLF);
            wo.setAge(w.getAge());
            wo.setOwner(player);
            wo.setHealth(w.getHealth());
            wo.setTamed(true);
            
            w.damage(200);
            AnimalHelper.hmWolfes.get(player).add(wo);
        }
        
        for(Ocelot o : respawnCats)
        {
            Ocelot oc = (Ocelot) player.getWorld().spawnCreature(player.getLocation(), EntityType.OCELOT);
            oc.setCatType(o.getCatType());
            oc.setAge(o.getAge());
            oc.setOwner(player);
            oc.setHealth(o.getHealth());
            oc.setTamed(true);
            
            o.damage(200);
            AnimalHelper.hmCats.get(player).add(oc);
        }
    }
    
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e)
    {
        AnimalHelper.countAnimals(e.getPlayer());
    }
    
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e)
    {
        AnimalHelper.hmWolfes.remove(e.getPlayer());
        AnimalHelper.hmCats.remove(e.getPlayer());
    }
    
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e)
    {
        Block block = e.getClickedBlock();
        
        if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && SignHelper.isValidSkyAdoptSign(block))
        {
            Sign sign = (Sign) block.getState();
            
            if(SignHelper.isWolf(sign))
            {
                adoptWolf(e.getPlayer(), sign);
            }
            else if(SignHelper.isCat(sign))
            {
                adoptCat(e.getPlayer(), sign);
            }
        }
    }
    
    public void adoptWolf(Player p, Sign sign)
    {
        if(skyAdopt.permission.has(p, skyAdopt.PERM_ADMIN))
        {
            spawnWolf(p, sign);
        }
        else if(skyAdopt.permission.has(p, skyAdopt.PERM_NOLIMIT))
        {
            if(skyAdopt.economy.has(p.getName(), SignHelper.getPrice(sign)))
            {
                spawnWolf(p, sign);
                skyAdopt.economy.withdrawPlayer(p.getName(), SignHelper.getPrice(sign));
            }
            else
                MessageHelper.sendMessageToPlayer(p, "Du hast nicht genügend Geld.", false);
        }
        else if(skyAdopt.permission.has(p, skyAdopt.PERM_ADOPT))
        {
            if(AnimalHelper.hmWolfes.get(p).size() < ConfigurationHelper.getMaxNumberOfWolfes(plugin))
            {
               if(skyAdopt.economy.has(p.getName(), SignHelper.getPrice(sign)))
               {
                   spawnWolf(p, sign);
                   skyAdopt.economy.withdrawPlayer(p.getName(), SignHelper.getPrice(sign));
               }
               else
                MessageHelper.sendMessageToPlayer(p, "Du hast nicht genügend Geld.", false);
            }
            else
                MessageHelper.sendMessageToPlayer(p, "Du hast genug Hunde.", false);
        }
        else
            MessageHelper.sendMessageToPlayer(p, "Du besitzt nicht die nötigen Permissions.", false);
    }
    
    public void spawnWolf(Player p, Sign sign)
    {
        Location loc = sign.getLocation();
        
        Wolf w = (Wolf) p.getWorld().spawnCreature(loc, EntityType.WOLF);
        w.setAdult();
        w.setOwner(p);
        w.setHealth(20);
        w.setTamed(true);
        
        AnimalHelper.hmWolfes.get(p).add(w);
    }
    
    public void adoptCat(Player p, Sign sign)
    {
        if(skyAdopt.permission.has(p, skyAdopt.PERM_ADMIN))
        {
            spawnCat(p, sign);
        }
        else if(skyAdopt.permission.has(p, skyAdopt.PERM_NOLIMIT))
        {
            if(skyAdopt.economy.has(p.getName(), SignHelper.getPrice(sign)))
            {
                spawnCat(p, sign);
                skyAdopt.economy.withdrawPlayer(p.getName(), SignHelper.getPrice(sign));
            }
            else
                MessageHelper.sendMessageToPlayer(p, "Du hast nicht genügend Geld.", false);
        }
        else if(skyAdopt.permission.has(p, skyAdopt.PERM_ADOPT))
        {
            if(AnimalHelper.hmCats.get(p).size() < ConfigurationHelper.getMaxNumberOfCats(plugin))
            {
               if(skyAdopt.economy.has(p.getName(), SignHelper.getPrice(sign)))
               {
                   spawnCat(p, sign);
                   skyAdopt.economy.withdrawPlayer(p.getName(), SignHelper.getPrice(sign));
               }
               else
                MessageHelper.sendMessageToPlayer(p, "Du hast nicht genügend Geld.", false);
            }
            else
                MessageHelper.sendMessageToPlayer(p, "Du hast genug Katzen.", false);
        }
        else
            MessageHelper.sendMessageToPlayer(p, "Du besitzt nicht die nötigen Permissions.", false);
    }
    
    public void spawnCat(Player p, Sign sign)
    {
        Location loc = sign.getLocation();
        
        Ocelot o = (Ocelot) p.getWorld().spawnCreature(loc, EntityType.OCELOT);
        setCatType(o);
        o.setAdult();
        o.setOwner(p);
        o.setHealth(10);
        o.setTamed(true);
        
        AnimalHelper.hmCats.get(p).add(o);
    }
    
    public void setCatType(Ocelot o)
    {
        int i = (int) (Math.random() * (4 - 1) + 1);
        
        if(i == 1)
            o.setCatType(Ocelot.Type.BLACK_CAT);
        else if(i == 2)
            o.setCatType(Ocelot.Type.RED_CAT);
        else if(i == 3)
            o.setCatType(Ocelot.Type.SIAMESE_CAT);
    }
}
